#include <iostream>
#include <cmath>

#include <boost/random.hpp>
#include <boost/thread.hpp>

#include <IL/il.h>

#include "Camera.hpp"
#include "scene.hpp"
#include "misc.hpp"
#include "ThreadFuncWork.hpp"

#include "primitive.hpp"
#include "light.hpp"

#include "parser.hpp"

using std::cout;
using std::endl;

int main ( int argc, char** argv )
{
    Scene       _scene;
    config_ns::ParseConfig( config, _scene);


    Camera      _camera;
    
    boost::thread_group threadsWorkers;
    
    ThreadFuncWork *funcs =( ThreadFuncWork*) new ThreadFuncWork[ config.nThread];

    
    for ( int i = 0; i < config.nThread; i++)
    {
        Task_t task;
        task.row_from = ( i    )*config.height_px/config.nThread;
        task.row_to   = ( i + 1)*config.height_px/config.nThread;
        
        funcs[ i].Init( task, _scene, _camera);
        
        if ( threadsWorkers.create_thread( funcs[i]) == NULL)
        {
            cout << "Can't create " << i+1 << " threads. " << i << " will be used." << endl;
            config.nThread = i;
            break;
        }
    }
    
    threadsWorkers.join_all();
    
    
    delete[] funcs;

    _camera.SaveImage( config.output_file);
    
    return 0;
}